module lang.d.dnamesvalidator;

import lang.inamesvalidator;
import lang.d.lexer.dlexer;
import lang.d.tokens.tokentype;
import lang.d.tokens.token;
import lang.d.tokens.patterns;

class DNamesValidator: INamesValidator {

    // TODO: make it shared
    private static DLexer LEXER;

    static this() {
        LEXER = new DLexer();
    }

    override public static bool isIdentifier(in string name) shared {
        LEXER.analize(name);
        LEXER.nextToken();
        if (LEXER.getTokenType() != TokenType.IDENTIFIER) {
            return false;
        }
        LEXER.nextToken();
        return ((LEXER.getTokenType() == TokenType.INVALID) || (LEXER.getTokenType() == TokenType.EOF));
    }

    override public static bool isKeyword(in string name) shared {
        LEXER.analize(name);
        LEXER.nextToken();
        if (!Patterns.isKeyword(LEXER.getTokenType())) {
            return false;
        }
        LEXER.nextToken();
        return ((LEXER.getTokenType() == TokenType.INVALID) || (LEXER.getTokenType() == TokenType.EOF));
    }

    unittest {
        assert(isIdentifier("fu23BygMKKkkkjVTv_jjf3_b"));
        assert(isIdentifier("____name"));
        assert(isIdentifier("             fu23BygMKKkkkjVTv_jjf3_b "));
        assert(!isIdentifier("____name   4"));
        assert(!isIdentifier("2id"));
        assert(isKeyword("int"));
        assert(isKeyword("void "));
        assert(isKeyword("   class     "));
        assert(!isKeyword("int a"));
        assert(!isKeyword("i s"));
    }
}
